<?php
/*
 * scrumbrella - Scrum board.
 *
 * Copyright (C) 2009  Gustavo N. Borgoni <gborgoni@voicemedia.com.ar>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

require_once("user.php");
require_once("role.php");
require_once("permission.php");
require_once("functions.php");
//require_once("project.php");
require_once("sprint.php");

/*! \brief return the userlist to be printed on html combobox */
function html_select_userlist() 
{
	$data = user_list();
	if (is_array($data)) {
		$ret = "<option value='0'>"._("Select an user")."</option>";
		foreach ($data AS $key => $user) {
			$ret .= "<option value='{$user['id_user']}'>{$user['name']} {$user['lastname']}</option>";
		}
	} else {
		$ret = "<option value='-1'>"._("No users registred")."</option>";
	}

	return $ret;
}

/*! \brief return the role list to be printed on html combobox */
function html_select_rolelist()
{
	$data = role_list();
	if (is_array($data)) {
		$i = 0;
		$ret[$i]['id_role'] = "";
		$ret[$i]['name']    = _("Select role");
		foreach ($data AS $key => $role) {
			$i++;
			$ret[$i]['id_role'] = $role['id_role'];
			$ret[$i]['name']    = $role['name'];
		}
	} else {
		$ret[0]['id_role'] = "";
		$ret[0]['name']    = _("No roles registred");
	}

	return $ret;
}

/*! \brief return the access level list to be printed on html combobox */
function html_select_accesslevellist()
{
	$data = permission_accesslevel_list();
	if (is_array($data)) {
		$ret = "";
		foreach ($data AS $key => $accesslevel) {
			$ret .= "<option value='{$accesslevel['id_accesslevel']}'>{$accesslevel['name']}</option>";
		}
	} else {
		$ret = "<option value='-1'>"._("No access level defined")."</option>";
	}

	return $ret;
}

/*! \brief return the units list to be printed on html combobox */
function html_select_unitlist()
{
	$data = unit_list();
	if (is_array($data)) {
		$i = 0;
		$ret[$i]['id_unit'] = "";
		$ret[$i]['name']    = _("Unit list");
		foreach ($data AS $key => $unit) {
			$i++;
			$ret[$i]['id_unit'] = $unit['id_unit'];
			$ret[$i]['name']    = $unit['name'];
		}
	} else {
		$ret[0]['id_unit'] = "";
		$ret[0]['name']    = _("No unit registred");
	}

	return $ret;
}

/*! \brief return user list to be printed on html combobox 
 *  \param $project ID
 *  \retval string html tags
 */
function html_select_userproject($project, $selected=0) 
{
	$data = user_list_byproject($project);
	if (is_array($data)) {
		$i = 0;
		
		$ret[$i]['id_user'] = -1;
		$ret[$i]['name']    = _("User list");
		
		if ($selected == 0) {
			$ret[$i]['selected'] = 1;
		} else {
			$ret[$i]['selected'] = 0;
		}

		foreach ($data AS $key => $user) {
			$i++;

			$ret[$i]['id_user'] = $user['id_user'];
			$ret[$i]['name'] = $user['username']." ".$user['lastname'];

			if ($selected != 0 && $selected == $user['id_user']) {
				$ret[$i]['selected'] = 1;
			} else {
				$ret[$i]['selected'] = 0;
			}
		}
	} else {
		$ret[0]['id_user'] = -1;
		$ret[0]['name']    = _("No users registred");
	}

	return $ret;
}

/*! \brief return project list assigned to an user
 *  \param $user ID
 *  \retval < 0 Error
 *  \retval Array
 */
function html_select_projectbyuser($user, $selected=0)
{
	$data = project_list_byuser($user);
	if (is_array($data)) {
		$i = 0;
		$ret[$i]['id_project'] = "";
		$ret[$i]['name'] = _("Project list");
		if ($selected == 0) {
			$ret[$i]['selected'] = 1;
		} else {
			$ret[$i]['selected'] = 0;
		}
		foreach ($data AS $key => $project) {
			$i++;

			$ret[$i]['id_project'] = $project['id_project'];
			$ret[$i]['name']       = $project['name'];

			if ($selected != 0 && $selected == $project['id_project']) {
				$ret[$i]['selected']   = 1;
			} else {
				$ret[$i]['selected']   = 0;
			}	
		}
	} else {
		$ret[0]['id_project'] = "";
		$ret[0]['name'] = _("No projects registred");
	}

	return $ret;
}

/*! \brief return the user story
 *  \param $priority 
 *  \param $estimated time
 *  \param $title summary of story
 *  \param $description
 *  \param $user
 *  \param $class CSS Style
 *  \retval string with HTML code
 */
function html_story($priority, $estimated, $title, $description, $user, $class="story_pennding", $id=0) 
{
	$ret  = "<table width='100%' border='0' cellpadding='1' cellspacing='1'><tr><td>";
	$ret .= "<table width='100%' border='0' cellpadding='1' cellspacing='1' class='".$class."_table'>";
	$ret .= "<tr>";
	$ret .= "	<td style='cursor:hand' onClick='show(\"$id\");'>";
	$ret .= "	<table width='100%' border='0' cellpadding='1' cellspacing='1'>";
	$ret .= "	<tr>";
	$ret .= "		<td class='".$class."_td' align='center' width='25'>$priority</td>";
	$ret .= "		<td class='".$class."_td'>| $title</td>";
	$ret .= "		<td width='10' class='".$class."_td'>+</td>";
	$ret .= "	</tr>";
	$ret .= "	</table>";
	$ret .=	"	</td>";
	$ret .= "</tr>";
	$ret .= "<tr><td align='center' style='display:none' id='$id' name='$id'>";
	$ret .= "<table width='99%' border='0' cellpadding='0' cellspacing='0'>";
	$ret .= "<tr>";
	$ret .= "	<td class='".$class."_descript'>$description</td>";
	$ret .= "</tr>";
	$ret .= "<tr><td height='10'></td></tr>";
	$ret .= "<tr>";
	$ret .= "	<td>";
	$ret .= "	<table width='100%' border='0' cellpadding='0' cellspacing='0'>";
	$ret .= "	<tr>";
	$ret .= "		<td class='".$class."_descript' height='20'>&nbsp;$user</td>";
	$ret .= "		<td class='".$class."_descript' width='30' align='center'>$estimated</td>";
	$ret .= "	</tr>";
	$ret .= "	</table>";
	$ret .= "	</td>";
	$ret .= "</tr>";
	$ret .= "</table></td></tr>";
	$ret .= "</table>";
	$ret .= "</tr></td></table>";
	return $ret;
}

/*! \brief return html select code with sprint list 
 *  \param $project ID
 *  \retval String html code
 */
function html_select_sprints($project)
{
	$data = sprint_list_by_project($project);
	if (is_array($data)) {
		$ret = "<option value='0'>"._("Select a sprint")."</option>";
		$i   = 1;
		foreach ($data AS $key => $sprint) {
			if ($i == count($data)) {
				$ret .= "<option value='{$sprint['id_sprint']}' selected>{$sprint['number']}</option>";
			} else {
				$ret .= "<option value='{$sprint['id_sprint']}'>{$sprint['number']}</option>";
			}
			$i++;
		}
	} else {
		$ret = "<option value='-1'>"._("No sprints registred")."</option>";
	}

	return $ret;
}

/*! \brief return html code for complete combobox control
 *  \param $project ID
 *  \retval string html code
 */
function html_select_pbacklog($project, $user)
{
	$data = project_backlog_user_list($project, $user);
	if (is_array($data)) {
		$i = 0;
		$ret[$i]['id_pbacklog'] = "";
		$ret[$i]['summary']    = _("Select project backlog");
		foreach ($data AS $key => $pbacklog) {
			$i++;
			$ret[$i]['id_pbacklog'] = $pbacklog['id_pbacklog'];
			$ret[$i]['summary']    = $pbacklog['summary'];
		}
	} else {
		$ret[0]['id_pbacklog'] = "";
		$ret[0]['summary']     = _("No project backlog registred");
	}

	return $ret;
}

/*! \brief return status list for combo box */
function html_select_statuslist()
{
	$data = sprint_status_list();
	if (is_array($data)) {
		$ret = "<option value='0'>Story Status</option>";
		foreach ($data AS $key => $status) {
			$ret .= "<option value='{$status['id_status']}'>{$status['name']}</option>";
		}
	} else {
		$ret = "<option value='-1'>"._("No status are defined")."</option>";
	}

	return $ret;
}
?>
